package com.demo.my.Util;




import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BaseDAO {

    //增加  单个增加 批量增加
    //单个增加
    public int addOrupdOrdel(String sql,Object... obj)throws Exception{
        //获取Connection 连接
        Connection conn = DBUtil.getConnection();
        //获取 st对象 执行sql
        PreparedStatement st = conn.prepareStatement(sql);
        // 获取sql语句的参数对象
        ParameterMetaData metaData = st.getParameterMetaData();
        //获取参数个数
        int count = metaData.getParameterCount();
        //设置参数值
        if(count>0&& count==obj.length){
            for (int i = 0; i < count; i++) {
                st.setObject(i+1,obj[i]);
            }
        }
        //执行sql
        int i = st.executeUpdate();
        //关连接
        DBUtil.close(null,st,conn);
        return i;
    }

    public int[] addBatch(String sql, List<Object[]> list) throws Exception{
        //获取Connection连接
        Connection conn = DBUtil.getConnection();
        //获取st对象
        PreparedStatement st = conn.prepareStatement(sql);
        //设置参数
        ParameterMetaData metaData = st.getParameterMetaData();
        int count = metaData.getParameterCount();
        if(count>0 && count==list.get(0).length){
            for (Object[] arr : list) {
                for (int i = 0; i < count; i++) {
                    st.setObject(i+1,arr[i]);
                }
                //添加到批量
                st.addBatch();
            }
        }
        //批量执行
        int[] ints = st.executeBatch();
        DBUtil.close(null,st,conn);
        return ints;
    }

    public <T> T getOne(String sql,T o,Object... obj)throws Exception{
        // 获取Connection 连接
        Connection conn = DBUtil.getConnection();
        // 获取执行sql的对象
        PreparedStatement st = conn.prepareStatement(sql);
        ParameterMetaData metaData = st.getParameterMetaData();
        int count = metaData.getParameterCount();
        //设置参数
        if(count>0 && count==obj.length){
            for (int i = 0; i < count; i++) {
                st.setObject(i+1,obj[i]);
            }
        }
        //执行sql 获取结果集
        ResultSet rs = st.executeQuery();
        ResultSetMetaData rsData = rs.getMetaData();
        while (rs.next()){
            int count1 = rsData.getColumnCount();
            for (int i = 1; i <= count1; i++) {
                String columnName = rsData.getColumnName(i);
                String entityFieldName =stringJoint(rsData.getColumnName(i));
                Object columnValue = rs.getObject(columnName);
                // 反射
                // class Field
                Class aClass = o.getClass();
                // 获取 o对象中所有属性
                Field field = aClass.getDeclaredField(entityFieldName);
                field.setAccessible(true);
                field.set(o,columnValue);
            }
        }
        DBUtil.close(rs,st,conn);
        return o;
    }



    public String stringJoint(String columnName){
        //判断是否有下划线
        StringBuffer buffer = new StringBuffer();

        if(columnName.contains("_")){
            //需要去除下划线首字母大写
            String[] split = columnName.split("_");
            buffer.append(split[0]);
            for(int i = 1;i<split.length;i++){
                String s = split[i];
                //首字母大写
                String newS = String.valueOf(s.charAt(0)).toUpperCase().concat(s.substring(1));
                buffer.append(newS);
            }
            return buffer.toString();
        }
        return buffer.append(columnName).toString();



    }

    // select * from hp_user where id >? --> 多条数据  封装成一个集合， 集合中存储这对象
    public <T> List<T> getList(String sql,T t, Object... obj)throws Exception{
        ArrayList<T> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        //设置参数
        ParameterMetaData metaData = st.getParameterMetaData();
        int count = metaData.getParameterCount();
        if(count>0 && count==obj.length){
            for (int i = 0; i < count; i++) {
                st.setObject(i+1,obj[i]);
            }
        }
        ResultSet rs = st.executeQuery();
        ResultSetMetaData rsData = rs.getMetaData();
        while (rs.next()){
            int count1 = rsData.getColumnCount();
            Class aClass1 = t.getClass();
            T o1 = (T)aClass1.newInstance();
            for (int i = 1; i <= count1; i++) {
                String entityFieldName = stringJoint(rsData.getColumnName(i));// 1,2,3,4
                String columnName = rsData.getColumnName(i);
                Object columnValue = rs.getObject(columnName);
                Class aClass = t.getClass();
                Field field = aClass.getDeclaredField(entityFieldName);
                field.setAccessible(true);
                field.set(o1,columnValue);
            }
            list.add(o1);
        }
        DBUtil.close(rs,st,conn);
        return list;
    }
    // select count(*) as num hp_user  --> 一个数据  int  一个变量
    public int getCount(String sql,Object... obj)throws Exception{
        Connection conn = DBUtil.getConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        ParameterMetaData metaData = st.getParameterMetaData();
        int count = metaData.getParameterCount();
        if(count>0 && count==obj.length){
            for (int i = 0; i < count; i++) {
                st.setObject(i+1,obj[i]);
            }
        }
        ResultSet rs = st.executeQuery();
        ResultSetMetaData rsData = rs.getMetaData();
        rs.next();
        String columnName = rsData.getColumnName(1);
        int num = rs.getInt(columnName);
        DBUtil.close(rs,st,conn);
        return num;
    }
}
